//
//  Sqlite.m
//  WebPDA
//
//  Created by Justin Yip on 8/5/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "Sqlite.h"
#import <sqlite3.h>


@implementation Sqlite

@synthesize connection;
@synthesize cache;


-(id)initWithPath:(NSString*)path {
	if (self == [super init]) {
		int returnCode;
		if (SQLITE_OK != (returnCode = sqlite3_open([path UTF8String], &connection))) {
			NSLog(@"error open database[%d], message: %s", returnCode, sqlite3_errmsg(connection));
			sqlite3_close(connection);
			abort();
		}
	}
	return self;
}

-(id)query:(NSString*)sql,... {
	va_list args;
	va_start(args,sql);
	
	sqlite3_stmt *stmt;
	int result_code = sqlite3_prepare_v2(connection, [sql UTF8String], -1, &stmt, 0);
	if (SQLITE_OK != result_code) {
		NSLog(@"error in prepare stmt:%@", sql);
		sqlite3_finalize(stmt);
		abort();
	}
	
	int bind_param_count = sqlite3_bind_parameter_count(stmt);
	
	//判断参数长度和sql需要的参数数目是否相等，并提示信息
	//if (bind_param_count != va) {}
	
	int index = 0;
	while (index < bind_param_count) {
		id obj = va_arg(args, id);
		[obj isMemberOfClass:[NSNumber class]];
		//sqlite3_bind_
		index++;
	}
	
	va_end(args);
	
	return nil;
}

-(void)dealloc {
	if (connection) {
		sqlite3_close(connection);
	}
	[cache release];
	[super dealloc];
}
@end
